package user

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"message-center/model"
	"net/http"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	var (
		rsp     = input.Gin{C: c}
		reqData = make(map[string]interface{})
	)
	secret := viper.GetString("jwt_secret")

	httpCode, errCode := rsp.ParamParse(&reqData)

	if httpCode != http.StatusOK {
		rsp.SendJSON(httpCode, errCode, nil)
		return
	}

	filter := map[string]interface{}{"username": reqData["username"].(string)}

	// 查询用户信息
	user, err := model.Get(filter)
	if err != nil {
		rsp.SendJSON(http.StatusInternalServerError, errno.ERROR, err)
		return
	}

	// 密码compare
	if err := util.Compare(reqData["password"].(string), user.PasswordHash); err != nil {
		fmt.Println("=======密码错误")
		rsp.SendJSON(http.StatusOK, errno.INVALID_PARAMS, nil)
		return
	}

	claims := jwt.MapClaims{
		"id":       user.Id,
		"username": user.Username,
		"nbf":      time.Now().Unix(),
		"iat":      time.Now().Unix(),
	}

	//Sign the json web token
	t, err := util.Sign(secret, claims)
	if err != nil {
		rsp.SendJSON(http.StatusInternalServerError, errno.ERROR, err)
		return
	}

	rsp.SendJSON(http.StatusOK, errno.SUCCESS, t)
}

// 退出登录
func Logout(c *gin.Context) {

	return
}

// 获取用户
func List(c *gin.Context) {

	return
}

// 获取用户
func Get(c *gin.Context) {
	var (
		rsp      = input.Gin{C: c}
		targetId = c.Param("id")
	)

	id, _ := strconv.Atoi(targetId)
	filter := map[string]interface{}{"id": id}
	user, err := model.Get(filter)
	if err != nil {
		rsp.SendJSON(http.StatusInternalServerError, errno.ERROR, nil)
		return
	}

	rsp.SendJSON(http.StatusOK, errno.SUCCESS, user)
	return
}

// 添加用户
func Create(c *gin.Context) {
	var (
		rsp     = input.Gin{C: c}
		reqData = make(map[string]interface{})
	)
	httpCode, errCode := rsp.ParamParse(&reqData)

	if httpCode != http.StatusOK {
		rsp.SendJSON(httpCode, errCode, nil)
		return
	}

	user := &model.UserModel{}

	params := CreateReqData{
		Username: reqData["username"].(string),
		Password: reqData["password"].(string),
	}

	user.Id = util.GenUuid(false)
	user.Username = params.Username
	user.PasswordHash, _ = util.Encrypt(params.Password)

	if err := model.Create(user); err != nil {
		rsp.SendJSON(http.StatusInternalServerError, errno.ERROR, err)
		return
	}

	rsp.SendJSON(http.StatusOK, errno.SUCCESS, user)
	return
}

//修改用户
func Modify(c *gin.Context) {
	var (
		rsp      = input.Gin{C: c}
		reqData  = make(map[string]interface{})
		targetId = c.Param("id")
	)

	httpCode, errCode := rsp.ParamParse(&reqData)
	if httpCode != http.StatusOK {
		rsp.SendJSON(httpCode, errCode, nil)
		return
	}

	user := model.UserModel{}

	params := CreateReqData{
		Username: reqData["username"].(string),
		Password: reqData["password"].(string),
	}

	user.Username = params.Username
	user.PasswordHash = params.Password

	id, _ := strconv.Atoi(targetId)

	if err := model.Update(id, user); err != nil {
		rsp.SendJSON(http.StatusInternalServerError, errno.ERROR, nil)
		return
	}
	rsp.SendJSON(http.StatusOK, errno.SUCCESS, "hello world!")
}
